function follow(btn,uid1,uid2){
	if($(btn).hasClass('tStBtn002')){
		$.ajax({url:'/follow',
			    async:true,
			    dataType:'json',
			    type:'POST',
			    data:{'uid1':uid1,'uid2':uid2},
			    success: function(ret) {
			    	$(btn).attr({
			    		  value: '取消',
			    		  title: '取消关注'
			    	});
			    	$(btn).removeClass('tStBtn002').addClass('tQxStBtn002');
			    },
			    error: function() {
			    	alert("关注时发生错误，请稍后再试。");
			    }});
	}else{
		$.ajax({url:'/unfollow',
		    async:true,
		    dataType:'json',
		    type:'POST',
		    data:{'uid1':uid1,'uid2':uid2},
		    success: function(ret) {
		    	$(btn).attr({
		    		  value: '关注',
		    		  title: '关注此人'
		    	});
		    	$(btn).removeClass('tQxStBtn002').addClass('tStBtn002');
		    },
		    error: function() {
		    	alert("取消关注时发生错误，请稍后再试。");
		    }});		
	}
}

function destroy(tid){
	$.post("/destroy",{'tid':tid},function(data){
		$('#'+tid).remove();
		var count = parseInt($('#tweets-count').html())-1;  
		$('#tweets-count').html(count);
	});
}
function format_time(time){
	var date = new Date(time*1000); 
	var month = date.getMonth()+1;
	var day = date.getDate();
	var hour = date.getHours();
	var minute = date.getMinutes();
	if (month<10) { month = '0'+month;}
	if (day<10){ day = '0'+day;} 
	if (hour<10){ hour = '0'+hour;} 
	if (minute<10){ minute = '0'+minute;} 
	var timestr = $.sprintf("%s月%s日 %s:%s",month,day,hour,minute);
	return timestr;
}
function tweet(){ 
	var text = $('#t-text').val(); 
	$.post("/tweet",{'text':text},function(t){ 
		$('#tweets').prepend(t)	
		var count = parseInt($('#tweets-count').html())+1;  
		$('#tweets-count').html(count);	
	});
	$('#t-text').text('');
}
function add_tweet_item (t){
	$('#tweets').prepend(t)	
	var count = parseInt($('#tweets-count').html())+1;  
	$('#tweets-count').html(count);	
}
function comment(tid){ 
	var text = $('#c-text-'+tid).val(); 
	$.post("/comment",{'text':text,'tid':tid},function(c){  
		add_comment_item(tid,c);
	});
	$('#c-text-'+tid).text('');
}

function forward(tid){ 
	var text = $('#f-text-'+tid).val(); 
	$.post("tweet",{'text':text,'retid':tid},function(t){  
		add_tweet_item(t);
	});
	$('#f-text-'+tid).text('');
	$('#fw_box_'+tid).addClass('ui-helper-hidden');
}

function add_tag(tid,text){ 
	//var text = $('#tag-text-'+tid).val(); 
	$.post("tag_tweet",{'tid':tid,'tag':text},function(tag){
		var tag_html = 
			  '<li id="%(tid)s-%(name)s" onmouseover="tagItemOnMouseOver(this)" onmouseout="tagItemsOnMouseOut(this)" >'
			 +'<a href="/tagline/%(name)s" class="ui-corner-all">%(name)s</a>'
			 +'<a href="#" class="delIco" onclick="untag_tweet(this,\'%(tid)s\',\'%(name)s\');return false;" title="删除"	style="display: none;">&nbsp;</a>'
	         +'</li>';
		$("#include_tag_ul_"+tid).append($.vsprintf(tag_html,[{tid:tid,tagid:tag._id.$oid,name:text}]));
	});
	//$('#tag-text-'+tid).text('');
	//$('#tag_box_'+tid).addClass('ui-helper-hidden');
}

function add_tag_link_click(tid){
	/*
	$("#comment_box_"+tid).addClass('ui-helper-hidden');
	$("#fw_box_"+tid).addClass('ui-helper-hidden');
	
	var add_tag_box = $('#add_tag_box_'+tid);
	if(add_tag_box.hasClass('ui-helper-hidden')){
		add_tag_box.removeClass('ui-helper-hidden');
	}else{
		add_tag_box.addClass('ui-helper-hidden');
	}*/
	$.alerts.okButton = '确定';
	$.alerts.cancelButton = '取消';
	jPrompt('标签名称：', '', '添加标签对话框', function (r) {
		add_tag(tid,r);
	}
	);	
}

function show_add_tag_dlg(tid){
	
}

function fw_link_click(tid){
	
	$("#comment_box_"+tid).addClass('ui-helper-hidden');
	$("#add_tag_box_"+tid).addClass('ui-helper-hidden');
	
	var fw_box = $('#fw_box_'+tid);
	if(fw_box.hasClass('ui-helper-hidden')){
		fw_box.removeClass('ui-helper-hidden');
	}else{
		fw_box.addClass('ui-helper-hidden');
	}
	
}

function comment_link_click(tid){
	$("#fw_box_"+tid).addClass('ui-helper-hidden');
	$("#add_tag_box_"+tid).addClass('ui-helper-hidden');
	
	var comment_box = $('#comment_box_'+tid);
	if(comment_box.hasClass('ui-helper-hidden')){
		comment_box.removeClass('ui-helper-hidden');
		
		//display comments
		var comment_list__ul = $('#comment_list__ul_'+tid);
		comment_list__ul.empty();
		
		$.post("comments",{'tid':tid},function(comments){
			if(comments.length == 0){
				$("#comment_list_box_"+tid).addClass('ui-helper-hidden');
			}else{
				$("#comment_list_box_"+tid).removeClass('ui-helper-hidden');
				$('#comment_cnt_span_'+tid).html(comments.length);
				var comment_html_template = 
						  '<li>'
				    	 +	'<a href="/%(_id)s">%(user_name)s</a>:&nbsp;<span>%(text)s</span>&nbsp;<span class="gray01">%(time)s</span>'
				         + '</li>';
				$.each(comments,function(i, c){
					var item = $.vsprintf(comment_html_template, [{_id: c._id.$oid, text: c.text, time: format_time(c.time), user_name: c.user.name, user_uid: c.user.uid}]);
					comment_list__ul.prepend(item)
				});
			}
			
		});
	}else{
		comment_box.addClass('ui-helper-hidden');
	}
	
}
function del_link_click(link,tid){ 
	$(link).fastConfirm({
		position: "top",
		questionText: "确定删除这条微博？",
		proceedText: "确定",
		cancelText: "取消",
		onProceed: function(trigger) {
			$.post("destroy",{'tid':tid});
			$(trigger).fastConfirm('close');
  		    $('#'+tid).remove();
		},
		onCancel: function(trigger) {
			$(trigger).fastConfirm('close');
		}
	});

}

function close_fw_box(tid){
	$("#fw_box_"+tid).addClass('ui-helper-hidden');
}

function close_comment_box(tid){
	$("#comment_box_"+tid).addClass('ui-helper-hidden');
}


function close_add_tag_box(tid){
	$("#add_tag_box_"+tid).addClass('ui-helper-hidden');
}


function add_comment_item (tid,c){
	$("#comment_list_box_"+tid).removeClass('ui-helper-hidden');
	var count = parseInt($('#comment_cnt_span_'+tid).html())+1; 
	$('#comment_cnt_span_'+tid).html(count);
	var comment_html_template = 
			  '<li>'
	    	 +	'<a href="/%(_id)s">%(user_name)s</a>:&nbsp;<span>%(text)s</span>&nbsp;<span class="gray01">%(time)s</span>'
	         + '</li>';
	var item = $.vsprintf(comment_html_template, [{_id: c._id.$oid, text: c.text, time: format_time(c.time), user_name: c.user.name, user_uid: c.user.uid}]);
	$('#comment_list__ul_'+tid).prepend(item)
}

function tagItemOnMouseOver(o) {
    $(o).find('.delIco').show();
}

function tagItemsOnMouseOut(o) {
	$(o).find('.delIco').hide();
}

function untag_tweet(link,tid,tag){
	var tag_link = $(link).siblings(".ui-corner-all");
	$(tag_link).fastConfirm({
		position: "top",
		questionText: "确定删除这个标签？",
		proceedText: "确定",
		cancelText: "取消",
		onProceed: function(trigger) {
			$.post('/untag_tweet',{'tid':tid,'tag':tag},function(data){
				if(data){
					//close dlg first,then remove dom
					$(trigger).fastConfirm('close');
					$("#"+tid+"-"+tag).remove();
				}
			});
		},
		onCancel: function(trigger) {
			$(trigger).fastConfirm('close');
		}
	});
}